//
//  SSWBNavigationController.swift
//  shanshanlol微博
//
//  Created by 叶竞泽 on 2017/1/7.
//  Copyright © 2017年 JingZe. All rights reserved.
//

import UIKit

class SSWBNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 隐藏navigationBar
        navigationBar.isHidden = true
    }

    /// 重写push方法
    ///
    /// - Parameters:
    ///   - viewController: 被push的控制器
    ///   - animated: animated description
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        // 隐藏底部的tabBar
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        // 判断控制器的类型
        if let vc = viewController as? SSWBBaseViewController {
            // 取出navItem
            // 如果是第一个push的页面,返回按钮的title叫上个控制器的title,如果是第二个或者后面的push,返回按钮的title是返回
            if childViewControllers.count > 1 {
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: "返回", target: self, action: #selector(goBack), isBack: true)
            } else if childViewControllers.count > 0 {
                let title: String = childViewControllers[childViewControllers.count - 1].title!
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(goBack), isBack: true)
            }
        }
        super.pushViewController(viewController, animated: true)
    }
    
    /// pop返回到上一级控制器
    @objc fileprivate func goBack() {
        popViewController(animated: true)
    }

}
